package org.dromara.app.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.app.domain.SkAppUser;
import org.dromara.app.domain.SkYueJuUser;
import org.dromara.app.domain.vo.SkAppUserVo;
import org.dromara.app.domain.vo.SkYueJuUserVo;
import org.dromara.app.service.ISkAppUserService;
import org.dromara.app.service.ISkYueJuUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.app.domain.vo.SkYueJuVo;
import org.dromara.app.domain.bo.SkYueJuBo;
import org.dromara.app.service.ISkYueJuService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 约局信息
 *
 * @author Evacuation
 * @date 2024-07-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/SkYueJu")
public class SkYueJuController extends BaseController {

    private final ISkYueJuService skYueJuService;

    private final ISkYueJuUserService iSkYueJuUserService;

    private final ISkAppUserService iSkAppUserService;
    /**
     * 查询约局信息列表
     */
    @SaCheckPermission("app:SkYueJu:list")
    @GetMapping("/list")
    public TableDataInfo<SkYueJuVo> list(SkYueJuBo bo, PageQuery pageQuery) {
        return skYueJuService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出约局信息列表
     */
    @SaCheckPermission("app:SkYueJu:export")
    @Log(title = "约局信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SkYueJuBo bo, HttpServletResponse response) {
        List<SkYueJuVo> list = skYueJuService.queryList(bo);
        ExcelUtil.exportExcel(list, "约局信息", SkYueJuVo.class, response);
    }

    /**
     * 获取约局信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("app:SkYueJu:query")
    @GetMapping("/{id}")
    public R<SkYueJuVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        SkYueJuVo vo = skYueJuService.queryById(id);
        //参加约局的用户
        List<SkYueJuUserVo> list = iSkYueJuUserService.getYueJuUserByYueJiId(vo.getId());
        vo.setList(list);
        //发起人信息
        SkAppUserVo user = iSkAppUserService.queryById(vo.getUserId());
        vo.setUserVo(user);
        return R.ok(vo);
    }

    /**
     * 新增约局信息
     */
    @SaCheckPermission("app:SkYueJu:add")
    @Log(title = "约局信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SkYueJuBo bo) {
        return toAjax(skYueJuService.insertByBo(bo));
    }

    /**
     * 修改约局信息
     */
    @SaCheckPermission("app:SkYueJu:edit")
    @Log(title = "约局信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SkYueJuBo bo) {
        return toAjax(skYueJuService.updateByBo(bo));
    }

    /**
     * 修改约局信息
     */
    @SaCheckPermission("app:SkYueJu:edit")
    @Log(title = "约局信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateisHall")
    public R<Void> updateisHall(String id) throws Exception {
        return toAjax(skYueJuService.updateisHall(id));
    }

    /**
     * 审核约局
     *
     * @param id
     * @return
     */
    @SaCheckPermission("app:SkYueJu:edit")
    @Log(title = "约局信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateisHalljujue")
    public R<Void> updateisHalljujue(String id) {
        return toAjax(skYueJuService.updateisHalljujue(id));
    }

    /**
     * 删除约局信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("app:SkYueJu:remove")
    @Log(title = "约局信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(skYueJuService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 待审核约局
     */
    @GetMapping("/getShList")
    public R<List<SkYueJuVo>> getShList() {
        return R.ok(skYueJuService.getShList());
    }

}
